package com.xjl.app.mq.sender;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class AiAlarmTestSender {

    @Value("${selector.ai.alarm:}")
    private String taget;

    @Value("${topic.ai.alarm:}")
    private String topic;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public void sendMessage(String msg){

        try {
            rocketMQTemplate.asyncSend(topic+taget,msg,new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    System.out.println(topic+taget+" 消息发送成功!!");
                    System.out.println("message :"+msg);
                }

                @Override
                public void onException(Throwable throwable) {
                    System.out.println(topic+taget+" 消息发送失败!!");
                    System.out.println(throwable.getMessage());
                }
            });
        }catch (Exception e){
            log.error("消息发送失败: {}",e.getMessage());
        }
    }

    public boolean sendAndCallBack(String msg){

        try {
            rocketMQTemplate.asyncSend(topic+taget,msg,new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    log.info(topic+taget+" 消息发送成功!!");
                    log.info("message :"+msg);
                }

                @Override
                public void onException(Throwable throwable) {
                    log.info(topic+taget+" 消息发送失败!!");
                    log.info(throwable.getMessage());
                }
            });
            return true;
        }catch (Exception e){
            log.error("消息发送失败: {}",e.getMessage());
        }
        return false;
    }

}
